//
//  GameScene.h
//  kasama1
//
//  Created by 丁逸鹏 on 15/3/3.
//
//

#ifndef __kasama1__GameScene__
#define __kasama1__GameScene__

#include "cocos2d.h"
#include "editor-support/cocostudio/CCSGUIReader.h"
#include "ui/CocosGUI.h"
#include "VisibleRect.h"
#include "Entity.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

#include "platform/android/jni/JniHelper.h"
#endif
#include "ddqueue.h"
#include "HelloWorldScene.h"
#include "Global.h"
#include <iostream>
#include <math.h>
#include "GoalNode.h"

using namespace cocos2d;
using namespace cocos2d::ui;
using namespace cocostudio;
#define MAX_HEIGHT 10
#define MAX_WIDTH 10
class GameScene:public Layer
{
public:
    static cocos2d::Scene* createScene();
    
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();
    CREATE_FUNC(GameScene);
    Point  tileCoordForPosition(Point pos);
    void updategame(float dt);
    void updatescore(float dt);
    void checkIsAllCanRemove(float dt);
    bool isCanRemoveSuccess(Entity *entity);
    void startGame(Ref *pSender,TouchEventType type);
    void pauseDialogCallBack(Ref *pSender,TouchEventType type);
    void downPartriesCallBack(Ref *pSender,TouchEventType type);
    void playCallBack(Ref *pSender,TouchEventType type);
    void continueGame(Ref *pSender,TouchEventType type);
    void BacktoHome(Ref *pSender,TouchEventType type);
    void changeMusicState(Ref* sender,CheckBoxEventType type);
    void CreateAndDrop(int row,int col);
    void CheckAndRemove();
    TMXTiledMap *map;
    TMXLayer *layer;
    Vector<Entity*> entities;
    Widget * startDialog;
    Widget * winDialog;
    Widget * falseDialog;
    Widget * pauseDialog;
    GoalNode ** goalnodelist;
    SpriteBatchNode *spriteSheet;
    LabelAtlas * remainLabel;
    LabelAtlas * scoreLabel;
    Sprite * scoresprite;
    bool m_isAnimationing;
    //程序锁，当精灵们都乖乖的不动时，程序就会开启它，让你可以安心地执行消除检测工作；
    //当有精灵调皮的时候，程序便会锁上它，避免你在这过程中对精灵”胡作非为”。
    bool isbegin;
    Entity *** entitylist;
    
    void getColChain(Entity * node,std::list<Entity *>& chainList);	//横向检察
    void getRowChain(Entity * node,std::list<Entity *> &chainList);
    void markRemove(Entity *entity);
    void dialogcloseCallBack();
    void fillPosition();
    void winCallBack();
    
    std::vector<Vec2> removedlist;
    
    virtual void keyBackClicked(EventKeyboard::KeyCode keyCode, Event* event);
    void sliderchangecallBack(Ref * sender,SliderEventType type);
    bool isCanSwap(Entity *entity1,Entity * entity2);
    bool isNeedRemove(Entity *entity1,Entity * entity2);
    void swap(Entity *entity1,Entity * entity2);
    DDqueue <Entity*> selectedEntities;
    void explode(Point p);
    
    int score;
    int remain;
    bool isRun;
    void CallAd();
};

#endif /* defined(__kasama1__GameScene__) */
